var app = app || {};
app.easeOut = {};
app.easeOut.ballX = 150;
app.easeOut.ballY = 150;
app.easeOut.duration = 0.5;
app.easeOut.fps = 24;
app.easeOut.move;

(function($) {

	$(function() {
		var $body = $('body');
		$body.bind('touchstart', touchPoint);
		$body.bind('click', clickPoint);
		$body.bind('touchmove', movePoint);
	});

	function clickPoint(e) {
		follow(event.pageX, event.pageY);
	}

	function touchPoint(e) {
		var touch = event.touches[0];
		follow(touch.pageX, touch.pageY);
	}
	
	function movePoint(e){
		e.preventDefault();
		var touch = event.touches[0];
		follow(touch.pageX, touch.pageY);
	}
	
	function follow(x, y) {
		
		var eo = app.easeOut;
		clearInterval(eo.move);
		
		var vx = (x - eo.ballX) / (eo.duration * eo.fps * 0.5);
		var vy = (y - eo.ballY) / (eo.duration * eo.fps * 0.5);
		var ax = 0;
		var ay = 0;

		eo.ballX += vx;
		eo.ballY += vy;
		
		var $ball = $('#ball');
		$ball.css('left', eo.ballX - 15);
		$ball.css('top', eo.ballY - 15);

		eo.move = setInterval(function() {
			ax = vx / (eo.duration * eo.fps * 0.5);
			ay = vy / (eo.duration * eo.fps * 0.5);
			vx -= ax;
			vy -= ay;
			eo.ballX += vx;
			eo.ballY += vy;

			$ball.css('left', eo.ballX - 15);
			$ball.css('top', eo.ballY - 15);

			if ((vx * vx) < 0.1 && (vy * vy) < 0.1) {
				clearInterval(eo.move);
			}
		}, (1000 / eo.fps));
	}
})(jQuery);